package me.flyray.biz.thymeleaf.modules.goods.rest;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.msg.TableResultResponse;
import com.github.wxiaoqi.security.common.qr.request.QrGoodsRequestParam;
import com.github.wxiaoqi.security.common.qr.request.QrQueryGoodsParam;
import com.github.wxiaoqi.security.common.util.ResponseHelper;

import me.flyray.biz.thymeleaf.api.BaseController;
import me.flyray.biz.thymeleaf.modules.goods.biz.QrGoodsInfoBiz;
import me.flyray.biz.thymeleaf.modules.goods.entity.QrGoodsInfo;


/**
 * 商品
 * @author Administrator
 *
 */
@Controller
@RequestMapping("qrGoodsInfos")
public class QrGoodsInfoController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(QrGoodsInfoController.class);

	@Autowired
	private QrGoodsInfoBiz infoBiz;
	
	/**
	 * 查询商品列表
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/list", method=RequestMethod.POST)
	public TableResultResponse<QrGoodsInfo> query(@RequestBody QrQueryGoodsParam param) {
		logger.info("查询商品列表。。。{}"+param);
		return infoBiz.queryList(param);
	}
	
	
	/**
	 * 添加
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/add", method=RequestMethod.POST)
	public Map<String, Object> add(@RequestBody QrGoodsRequestParam param) {
		logger.info("添加商品。。。{}"+param);
		QrGoodsInfo goodsInfo = infoBiz.addGoodsInfo(param);
		if (goodsInfo == null) {
			return ResponseHelper.success(null, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
		}else {
			return ResponseHelper.success(null, null, ResponseCode.FUNCTION_EXIST.getCode(), ResponseCode.FUNCTION_EXIST.getMessage());
		}
	}
	
	/**
	 * 导入
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/import", method=RequestMethod.POST)
	public Map<String, Object> importData(@RequestBody Map<String, Object> param) {
		logger.info("添加商品。。。{}"+param);
		infoBiz.importGoodsList(param);
		return ResponseHelper.success(null, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
	}
	
	/**
	 * 修改
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public Map<String, Object> update(@RequestBody QrGoodsRequestParam param) {
		logger.info("修改商品。。。{}"+param);
		QrGoodsInfo goodsInfo = infoBiz.addGoodsInfo(param);
		if (goodsInfo != null) {
			return ResponseHelper.success(null, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
		}else {
			return ResponseHelper.success(null, null, ResponseCode.FUNCTION_EXIST.getCode(), ResponseCode.FUNCTION_EXIST.getMessage());
		}
	}
	
	
	/**
	 * 删除
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public Map<String, Object> delete(@RequestBody QrGoodsRequestParam param) {
		logger.info("删除商品。。。{}"+param);
		QrGoodsInfo goodsInfo = infoBiz.deleteGoodsInfo(param);
		if (goodsInfo != null) {
			return ResponseHelper.success(null, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
		}else {
			return ResponseHelper.success(null, null, ResponseCode.FUNCTION_EXIST.getCode(), ResponseCode.FUNCTION_EXIST.getMessage());
		}
	}
	
	
	/**
	 * 生成二维码
	 * @param param
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/createQr", method=RequestMethod.POST)
	public Map<String, Object> createQr(@RequestBody Map<String, Object> param) {
		logger.info("生成二维码参数。。。{}"+param);
		Map<String, Object> res = infoBiz.createGoodsQr(param);
		if (res != null) {
			return ResponseHelper.success(res, null, ResponseCode.OK.getCode(), ResponseCode.OK.getMessage());
		}else {
			return ResponseHelper.success(null, null, ResponseCode.ABNORMAL_FIELDS.getCode(), ResponseCode.ABNORMAL_FIELDS.getMessage());
		}
	}
	
	
}